3 * Generator and manager of database load balancing objects
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 namespace Wikimedia\Rdbms
;
26 use Psr\Log\LoggerInterface
;
27 use Wikimedia\ScopedCallback
;
35 * An interface for generating database load balancers
38 abstract class LBFactory
implements ILBFactory
{
39 /** @var ChronologyProtector */
41 /** @var object|string Class name or object With profileIn/profileOut methods */
43 /** @var TransactionProfiler */
44 protected $trxProfiler;
45 /** @var LoggerInterface */
46 protected $replLogger;
47 /** @var LoggerInterface */
48 protected $connLogger;
49 /** @var LoggerInterface */
50 protected $queryLogger;
51 /** @var LoggerInterface */
52 protected $perfLogger;
53 /** @var callable Error logger */
54 protected $errorLogger;
59 /** @var WANObjectCache */
62 /** @var DatabaseDomain Local domain */
63 protected $localDomain;
64 /** @var string Local hostname of the app server */
66 /** @var array Web request information about the client */
67 protected $requestInfo;
71 /** @var string|bool String if a requested DBO_TRX transaction round is active */
72 protected $trxRoundId = false;
73 /** @var string|bool Reason all LBs are read-only or false if not */
74 protected $readOnlyReason = false;
75 /** @var callable[] */
76 protected $replicationWaitCallbacks = [];
78 /** @var bool Whether this PHP instance is for a CLI script */
80 /** @var string Agent name for query profiling */
83 private static $loggerFields =
84 [ 'replLogger', 'connLogger', 'queryLogger', 'perfLogger' ];
86 public function __construct( array $conf ) {
87 $this->localDomain
= isset( $conf['localDomain'] )
88 ? DatabaseDomain
::newFromId( $conf['localDomain'] )
89 : DatabaseDomain
::newUnspecified();
91 if ( isset( $conf['readOnlyReason'] ) && is_string( $conf['readOnlyReason'] ) ) {
92 $this->readOnlyReason
= $conf['readOnlyReason'];
95 $this->srvCache
= isset( $conf['srvCache'] ) ?
$conf['srvCache'] : new EmptyBagOStuff();
96 $this->memStash
= isset( $conf['memStash'] ) ?
$conf['memStash'] : new EmptyBagOStuff();
97 $this->wanCache
= isset( $conf['wanCache'] )
99 : WANObjectCache
::newEmpty();
101 foreach ( self
::$loggerFields as $key ) {
102 $this->$key = isset( $conf[$key] ) ?
$conf[$key] : new \Psr\Log\
NullLogger();
104 $this->errorLogger
= isset( $conf['errorLogger'] )
105 ?
$conf['errorLogger']
106 : function ( Exception
$e ) {
107 trigger_error( E_USER_WARNING
, get_class( $e ) . ': ' . $e->getMessage() );
110 $this->profiler
= isset( $conf['profiler'] ) ?
$conf['profiler'] : null;
111 $this->trxProfiler
= isset( $conf['trxProfiler'] )
112 ?
$conf['trxProfiler']
113 : new TransactionProfiler();
115 $this->requestInfo
= [
116 'IPAddress' => isset( $_SERVER[ 'REMOTE_ADDR' ] ) ?
$_SERVER[ 'REMOTE_ADDR' ] : '',
117 'UserAgent' => isset( $_SERVER['HTTP_USER_AGENT'] ) ?
$_SERVER['HTTP_USER_AGENT'] : '',
118 'ChronologyProtection' => 'true',
119 'ChronologyPositionIndex' => isset( $_GET['cpPosIndex'] ) ?
$_GET['cpPosIndex'] : null
122 $this->cliMode
= isset( $conf['cliMode'] ) ?
$conf['cliMode'] : PHP_SAPI
=== 'cli';
123 $this->hostname
= isset( $conf['hostname'] ) ?
$conf['hostname'] : gethostname();
124 $this->agent
= isset( $conf['agent'] ) ?
$conf['agent'] : '';
126 $this->ticket
= mt_rand();
129 public function destroy() {
130 $this->shutdown( self
::SHUTDOWN_NO_CHRONPROT
);
131 $this->forEachLBCallMethod( 'disable' );
134 public function shutdown(
135 $mode = self
::SHUTDOWN_CHRONPROT_SYNC
, callable
$workCallback = null, &$cpIndex = null
137 $chronProt = $this->getChronologyProtector();
138 if ( $mode === self
::SHUTDOWN_CHRONPROT_SYNC
) {
139 $this->shutdownChronologyProtector( $chronProt, $workCallback, 'sync', $cpIndex );
140 } elseif ( $mode === self
::SHUTDOWN_CHRONPROT_ASYNC
) {
141 $this->shutdownChronologyProtector( $chronProt, null, 'async', $cpIndex );
144 $this->commitMasterChanges( __METHOD__
); // sanity
148 * @see ILBFactory::newMainLB()
149 * @param bool $domain
150 * @return LoadBalancer
152 abstract public function newMainLB( $domain = false );
155 * @see ILBFactory::getMainLB()
156 * @param bool $domain
157 * @return LoadBalancer
159 abstract public function getMainLB( $domain = false );
162 * @see ILBFactory::newExternalLB()
163 * @param string $cluster
164 * @return LoadBalancer
166 abstract public function newExternalLB( $cluster );
169 * @see ILBFactory::getExternalLB()
170 * @param string $cluster
171 * @return LoadBalancer
173 abstract public function getExternalLB( $cluster );
176 * Call a method of each tracked load balancer
178 * @param string $methodName
181 protected function forEachLBCallMethod( $methodName, array $args = [] ) {
183 function ( ILoadBalancer
$loadBalancer, $methodName, array $args ) {
184 call_user_func_array( [ $loadBalancer, $methodName ], $args );
186 [ $methodName, $args ]
190 public function flushReplicaSnapshots( $fname = __METHOD__
) {
191 $this->forEachLBCallMethod( 'flushReplicaSnapshots', [ $fname ] );
194 public function commitAll( $fname = __METHOD__
, array $options = [] ) {
195 $this->commitMasterChanges( $fname, $options );
196 $this->forEachLBCallMethod( 'commitAll', [ $fname ] );
199 public function beginMasterChanges( $fname = __METHOD__
) {
200 if ( $this->trxRoundId
!== false ) {
201 throw new DBTransactionError(
203 "$fname: transaction round '{$this->trxRoundId}' already started."
206 $this->trxRoundId
= $fname;
207 // Set DBO_TRX flags on all appropriate DBs
208 $this->forEachLBCallMethod( 'beginMasterChanges', [ $fname ] );
211 public function commitMasterChanges( $fname = __METHOD__
, array $options = [] ) {
212 if ( $this->trxRoundId
!== false && $this->trxRoundId
!== $fname ) {
213 throw new DBTransactionError(
215 "$fname: transaction round '{$this->trxRoundId}' still running."
218 /** @noinspection PhpUnusedLocalVariableInspection */
219 $scope = $this->getScopedPHPBehaviorForCommit(); // try to ignore client aborts
220 // Run pre-commit callbacks and suppress post-commit callbacks, aborting on failure
221 $this->forEachLBCallMethod( 'finalizeMasterChanges' );
222 $this->trxRoundId
= false;
223 // Perform pre-commit checks, aborting on failure
224 $this->forEachLBCallMethod( 'approveMasterChanges', [ $options ] );
225 // Log the DBs and methods involved in multi-DB transactions
226 $this->logIfMultiDbTransaction();
227 // Actually perform the commit on all master DB connections and revert DBO_TRX
228 $this->forEachLBCallMethod( 'commitMasterChanges', [ $fname ] );
229 // Run all post-commit callbacks
230 /** @var Exception $e */
231 $e = null; // first callback exception
232 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$e ) {
233 $ex = $lb->runMasterPostTrxCallbacks( IDatabase
::TRIGGER_COMMIT
);
236 // Commit any dangling DBO_TRX transactions from callbacks on one DB to another DB
237 $this->forEachLBCallMethod( 'commitMasterChanges', [ $fname ] );
238 // Throw any last post-commit callback error
239 if ( $e instanceof Exception
) {
244 public function rollbackMasterChanges( $fname = __METHOD__
) {
245 $this->trxRoundId
= false;
246 $this->forEachLBCallMethod( 'suppressTransactionEndCallbacks' );
247 $this->forEachLBCallMethod( 'rollbackMasterChanges', [ $fname ] );
248 // Run all post-rollback callbacks
249 $this->forEachLB( function ( ILoadBalancer
$lb ) {
250 $lb->runMasterPostTrxCallbacks( IDatabase
::TRIGGER_ROLLBACK
);
254 public function hasTransactionRound() {
255 return ( $this->trxRoundId
!== false );
259 * Log query info if multi DB transactions are going to be committed now
261 private function logIfMultiDbTransaction() {
263 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$callersByDB ) {
264 $masterName = $lb->getServerName( $lb->getWriterIndex() );
265 $callers = $lb->pendingMasterChangeCallers();
267 $callersByDB[$masterName] = $callers;
271 if ( count( $callersByDB ) >= 2 ) {
272 $dbs = implode( ', ', array_keys( $callersByDB ) );
273 $msg = "Multi-DB transaction [{$dbs}]:\n";
274 foreach ( $callersByDB as $db => $callers ) {
275 $msg .= "$db: " . implode( '; ', $callers ) . "\n";
277 $this->queryLogger
->info( $msg );
281 public function hasMasterChanges() {
283 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$ret ) {
284 $ret = $ret ||
$lb->hasMasterChanges();
290 public function laggedReplicaUsed() {
292 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$ret ) {
293 $ret = $ret ||
$lb->laggedReplicaUsed();
299 public function hasOrMadeRecentMasterChanges( $age = null ) {
301 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( $age, &$ret ) {
302 $ret = $ret ||
$lb->hasOrMadeRecentMasterChanges( $age );
307 public function waitForReplication( array $opts = [] ) {
312 'ifWritesSince' => null
315 if ( $opts['domain'] === false && isset( $opts['wiki'] ) ) {
316 $opts['domain'] = $opts['wiki']; // b/c
319 // Figure out which clusters need to be checked
320 /** @var ILoadBalancer[] $lbs */
322 if ( $opts['cluster'] !== false ) {
323 $lbs[] = $this->getExternalLB( $opts['cluster'] );
324 } elseif ( $opts['domain'] !== false ) {
325 $lbs[] = $this->getMainLB( $opts['domain'] );
327 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$lbs ) {
331 return; // nothing actually used
335 // Get all the master positions of applicable DBs right now.
336 // This can be faster since waiting on one cluster reduces the
337 // time needed to wait on the next clusters.
338 $masterPositions = array_fill( 0, count( $lbs ), false );
339 foreach ( $lbs as $i => $lb ) {
340 if ( $lb->getServerCount() <= 1 ) {
341 // T29975 - Don't try to wait for replica DBs if there are none
342 // Prevents permission error when getting master position
344 } elseif ( $opts['ifWritesSince']
345 && $lb->lastMasterChangeTimestamp() < $opts['ifWritesSince']
347 continue; // no writes since the last wait
349 $masterPositions[$i] = $lb->getMasterPos();
352 // Run any listener callbacks *after* getting the DB positions. The more
353 // time spent in the callbacks, the less time is spent in waitForAll().
354 foreach ( $this->replicationWaitCallbacks
as $callback ) {
359 foreach ( $lbs as $i => $lb ) {
360 if ( $masterPositions[$i] ) {
361 // The DBMS may not support getMasterPos()
362 if ( !$lb->waitForAll( $masterPositions[$i], $opts['timeout'] ) ) {
363 $failed[] = $lb->getServerName( $lb->getWriterIndex() );
369 throw new DBReplicationWaitError(
371 "Could not wait for replica DBs to catch up to " .
372 implode( ', ', $failed )
377 public function setWaitForReplicationListener( $name, callable
$callback = null ) {
379 $this->replicationWaitCallbacks
[$name] = $callback;
381 unset( $this->replicationWaitCallbacks
[$name] );
385 public function getEmptyTransactionTicket( $fname ) {
386 if ( $this->hasMasterChanges() ) {
387 $this->queryLogger
->error( __METHOD__
. ": $fname does not have outer scope.\n" .
388 ( new RuntimeException() )->getTraceAsString() );
393 return $this->ticket
;
396 public function commitAndWaitForReplication( $fname, $ticket, array $opts = [] ) {
397 if ( $ticket !== $this->ticket
) {
398 $this->perfLogger
->error( __METHOD__
. ": $fname does not have outer scope.\n" .
399 ( new RuntimeException() )->getTraceAsString() );
404 // The transaction owner and any caller with the empty transaction ticket can commit
405 // so that getEmptyTransactionTicket() callers don't risk seeing DBTransactionError.
406 if ( $this->trxRoundId
!== false && $fname !== $this->trxRoundId
) {
407 $this->queryLogger
->info( "$fname: committing on behalf of {$this->trxRoundId}." );
408 $fnameEffective = $this->trxRoundId
;
410 $fnameEffective = $fname;
413 $this->commitMasterChanges( $fnameEffective );
414 $this->waitForReplication( $opts );
415 // If a nested caller committed on behalf of $fname, start another empty $fname
416 // transaction, leaving the caller with the same empty transaction state as before.
417 if ( $fnameEffective !== $fname ) {
418 $this->beginMasterChanges( $fnameEffective );
422 public function getChronologyProtectorTouched( $dbName ) {
423 return $this->getChronologyProtector()->getTouched( $dbName );
426 public function disableChronologyProtection() {
427 $this->getChronologyProtector()->setEnabled( false );
431 * @return ChronologyProtector
433 protected function getChronologyProtector() {
434 if ( $this->chronProt
) {
435 return $this->chronProt
;
438 $this->chronProt
= new ChronologyProtector(
441 'ip' => $this->requestInfo
['IPAddress'],
442 'agent' => $this->requestInfo
['UserAgent'],
444 $this->requestInfo
['ChronologyPositionIndex']
446 $this->chronProt
->setLogger( $this->replLogger
);
448 if ( $this->cliMode
) {
449 $this->chronProt
->setEnabled( false );
450 } elseif ( $this->requestInfo
['ChronologyProtection'] === 'false' ) {
451 // Request opted out of using position wait logic. This is useful for requests
452 // done by the job queue or background ETL that do not have a meaningful session.
453 $this->chronProt
->setWaitEnabled( false );
456 $this->replLogger
->debug( __METHOD__
. ': using request info ' .
457 json_encode( $this->requestInfo
, JSON_PRETTY_PRINT
) );
459 return $this->chronProt
;
463 * Get and record all of the staged DB positions into persistent memory storage
465 * @param ChronologyProtector $cp
466 * @param callable|null $workCallback Work to do instead of waiting on syncing positions
467 * @param string $mode One of (sync, async); whether to wait on remote datacenters
468 * @param int|null &$cpIndex DB position key write counter; incremented on update
470 protected function shutdownChronologyProtector(
471 ChronologyProtector
$cp, $workCallback, $mode, &$cpIndex = null
473 // Record all the master positions needed
474 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( $cp ) {
475 $cp->shutdownLB( $lb );
477 // Write them to the persistent stash. Try to do something useful by running $work
478 // while ChronologyProtector waits for the stash write to replicate to all DCs.
479 $unsavedPositions = $cp->shutdown( $workCallback, $mode, $cpIndex );
480 if ( $unsavedPositions && $workCallback ) {
481 // Invoke callback in case it did not cache the result yet
482 $workCallback(); // work now to block for less time in waitForAll()
484 // If the positions failed to write to the stash, at least wait on local datacenter
485 // replica DBs to catch up before responding. Even if there are several DCs, this increases
486 // the chance that the user will see their own changes immediately afterwards. As long
487 // as the sticky DC cookie applies (same domain), this is not even an issue.
488 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( $unsavedPositions ) {
489 $masterName = $lb->getServerName( $lb->getWriterIndex() );
490 if ( isset( $unsavedPositions[$masterName] ) ) {
491 $lb->waitForAll( $unsavedPositions[$masterName] );
497 * Base parameters to LoadBalancer::__construct()
500 final protected function baseLoadBalancerParams() {
502 'localDomain' => $this->localDomain
,
503 'readOnlyReason' => $this->readOnlyReason
,
504 'srvCache' => $this->srvCache
,
505 'wanCache' => $this->wanCache
,
506 'profiler' => $this->profiler
,
507 'trxProfiler' => $this->trxProfiler
,
508 'queryLogger' => $this->queryLogger
,
509 'connLogger' => $this->connLogger
,
510 'replLogger' => $this->replLogger
,
511 'errorLogger' => $this->errorLogger
,
512 'hostname' => $this->hostname
,
513 'cliMode' => $this->cliMode
,
514 'agent' => $this->agent
,
515 'chronologyProtector' => $this->getChronologyProtector()
520 * @param ILoadBalancer $lb
522 protected function initLoadBalancer( ILoadBalancer
$lb ) {
523 if ( $this->trxRoundId
!== false ) {
524 $lb->beginMasterChanges( $this->trxRoundId
); // set DBO_TRX
528 public function setDomainPrefix( $prefix ) {
529 $this->localDomain
= new DatabaseDomain(
530 $this->localDomain
->getDatabase(),
535 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( $prefix ) {
536 $lb->setDomainPrefix( $prefix );
540 public function closeAll() {
541 $this->forEachLBCallMethod( 'closeAll', [] );
544 public function setAgentName( $agent ) {
545 $this->agent
= $agent;
548 public function appendShutdownCPIndexAsQuery( $url, $index ) {
550 $this->forEachLB( function ( ILoadBalancer
$lb ) use ( &$usedCluster ) {
551 $usedCluster |
= ( $lb->getServerCount() > 1 );
554 if ( !$usedCluster ) {
555 return $url; // no master/replica clusters touched
558 return strpos( $url, '?' ) === false ?
"$url?cpPosIndex=$index" : "$url&cpPosIndex=$index";
561 public function setRequestInfo( array $info ) {
562 $this->requestInfo
= $info +
$this->requestInfo
;
566 * Make PHP ignore user aborts/disconnects until the returned
567 * value leaves scope. This returns null and does nothing in CLI mode.
569 * @return ScopedCallback|null
571 final protected function getScopedPHPBehaviorForCommit() {
572 if ( PHP_SAPI
!= 'cli' ) { // https://bugs.php.net/bug.php?id=47540
573 $old = ignore_user_abort( true ); // avoid half-finished operations
574 return new ScopedCallback( function () use ( $old ) {
575 ignore_user_abort( $old );
582 function __destruct() {
587 class_alias( LBFactory
::class, 'LBFactory' );